﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Agency_Update_waitPriceState : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    public string strConditions;
    public int projectid;
    protected void Page_Load(object sender, EventArgs e)
    {

        string strSql;
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            if (projectid != 10)
            {
                showGridView();
            }
            else
            {
                this.DlAttorney.Visible = false;
                GridView();

            }
           
        }

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods";                                               //要查询的表明
        dbPage.Key = "SupplyGoodsId";                                          //表的主键    
        dbPage.Where = strConditions;

        dbPage.SelectField = "Factor,SupplyGoodsId,projectid,productcode,ProductName,MedicineModel,Outlookc,GoodsId,CompanyName_PS,CompanyName_SC,CompanyName_TB,BidPrice,LatestPrice,pack,GoodsName,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels";
        //要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 15;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName_SC,ProductName";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        DlAttorney.DataSource = dr;
        DlAttorney.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }

    public void GridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods";                                               //要查询的表明
        dbPage.Key = "SupplyGoodsId";                                          //表的主键    
        dbPage.Where = strConditions;

        dbPage.SelectField = "RetailPrice,MEMO4,Factor,SupplyGoodsId,projectid,productcode,ProductName,MedicineModel,Outlookc,GoodsId,CompanyName_PS,CompanyName_SC,CompanyName_TB,BidPrice,LatestPrice,pack,GoodsName,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels";
        //要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 15;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName_SC,ProductName";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        this.DataList1.DataSource = dr;
        this.DataList1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", ProductName = "", _ProductCode,ProductNameX = "";
        int _GoodsID = 1;
        if (Convert.ToInt32(Request.QueryString["projectid"]) == 10)
        {
            strtemp = strtemp + " projectid=" + projectid + "and (waitState=1 or waitState1=1) and";
        }
        else
        {
            strtemp = strtemp + " projectid=" + projectid + "and waitState=1 and";
        }
        ProductName = txtProductName.Text.ToString().Trim();
        ProductNameX = TextBox1.Text.Trim();
        _ProductCode = txtProductCode.Text.ToString().Trim();
        if (Text_GoodsID.Text.ToString().Trim().Length != 0)
        {
            _GoodsID = Convert.ToInt32(Text_GoodsID.Text);
        }



        if (_ProductCode != "")
        {
            strtemp = strtemp + " ProductCode like '%" + _ProductCode + "%' and";
        }
        if (ProductName != "")
        {
            strtemp = strtemp + "(ProductName like '%" + ProductName + "%' or Product_SpellingCode like'%" + ProductName + "%') and";
        }
         if (ProductNameX != "")
        {
            strtemp = strtemp + "(ProductName = '" + ProductNameX + "' or Product_SpellingCode ='" + ProductNameX + "') and";
        }
        if (Text_GoodsID.Text != "")
        {
            strtemp = strtemp + " GoodsID = '" + _GoodsID + "' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;
    }


    protected void Button2_Click(object sender, EventArgs e)//调整中标价格
    {
        if ((Convert.ToInt32(Request.QueryString["ProjectId"]) == 8) || (Convert.ToInt32(Request.QueryString["ProjectId"]) == 12) || (Convert.ToInt32(Request.QueryString["ProjectId"]) == 9))
        {
            string str = "", strSql, SupplyGoodsId = "";
            int nNumber = 0;
            Db db = new Db();
            System.Data.IDataReader dr;
            strSql = "select SupplyGoodsId from SupplyPlan_Goods where waitState=1 and  Projectid='"+projectid+"'";
            dr = db.DataReader(strSql);
            while (dr.Read()) 
            {
                nNumber++;
                SupplyGoodsId += SysFun.ToTrim(dr["SupplyGoodsId"]) + ",";
            }
            dr.Close();
            dr.Dispose();
            if (SupplyGoodsId.Length > 2)
            {
                SupplyGoodsId = SupplyGoodsId.Remove(SupplyGoodsId.Length - 1);
                str = "update SupplyPlan_Goods set historyPrice =convert(varchar(1000),historyprice) +'|'+ convert(varchar(1000),BidPrice), historytime =convert(varchar(1000),historytime) +'|'+ convert(varchar(1000),getdate()) where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update SupplyPlan_Goods set BidPrice=convert(numeric(20,4),LatestPrice) where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update SupplyPlan_Goods set LatestPrice='',waitState=0 where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update tra_HosptaiPurchase_SupplyPlan_Goods set PurchasePrice=BidPrice where  PurchasePrice>BidPrice and supplyGoodsId in (" + SupplyGoodsId + ")";
                db.Command(str);


                Response.Write("<script>alert('中标价格修改成功！！！影响" + nNumber.ToString() + "条记录');</script>");
                Button2.Enabled = false;
            }
            else
            {
                Response.Write("<script>alert('中标价格已修改！');</script>");
            }

            db.Close();
        }
        //if(Convert.ToInt32(Request.QueryString["ProjectId"]) == 9)
        //{
        //    string str = "", strSql, Goodsid = "";
        //    int nNumber = 0;
        //    Db db = new Db();
        //    System.Data.IDataReader dr;
        //    strSql = "select Goodsid from SupplyPlan_Goods where waitState=1 and  Projectid='"+projectid+"'";
        //    dr = db.DataReader(strSql);
        //    while (dr.Read()) 
        //    {
        //        nNumber++;
        //        Goodsid += SysFun.ToTrim(dr["Goodsid"]) + ",";
        //    }
        //    dr.Close();
        //    dr.Dispose();
        //    if (Goodsid.Length > 2)
        //    {
        //        Goodsid = Goodsid.Remove(Goodsid.Length - 1);
        //        str = "update SupplyPlan_Goods set historyPrice =convert(varchar(1000),historyprice) +'|'+ convert(varchar(1000),BidPrice), historytime =convert(varchar(1000),historytime) +'|'+ convert(varchar(1000),getdate()) where Goodsid in (" + Goodsid + ")";
        //        str += " update SupplyPlan_Goods set BidPrice=convert(numeric(20,4),LatestPrice) where supplyGoodsId in (" + Goodsid + ")";
        //        str += " update SupplyPlan_Goods set LatestPrice='',waitState=0 where supplyGoodsId in (" + Goodsid + ")";
        //        str += " update tra_HosptaiPurchase_SupplyPlan_Goods set PurchasePrice=BidPrice where  PurchasePrice>BidPrice and supplyGoodsId in (" + Goodsid + ")";
        //        db.Command(str);


        //        Response.Write("<script>alert('中标价格修改成功！！！影响" + nNumber.ToString() + "条记录');</script>");
        //        Button2.Enabled = false;
        //    }
        //    else
        //    {
        //        Response.Write("<script>alert('中标价格已修改！');</script>");
        //    }

        //    db.Close();
        //}
        // if(Convert.ToInt32(Request.QueryString["ProjectId"]) == 9)
        //{
        //    string str = "", strSql, Goodsid = "";
        //    int nNumber = 0;
        //    Db db = new Db();
        //    System.Data.IDataReader dr;
        //    strSql = "select Goodsid from SupplyPlan_Goods where waitState=1 and  Projectid='"+projectid+"'";
        //    dr = db.DataReader(strSql);
        //    while (dr.Read()) 
        //    {
        //        nNumber++;
        //        Goodsid += SysFun.ToTrim(dr["Goodsid"]) + ",";
        //    }
        //    dr.Close();
        //    dr.Dispose();
            
            
        //    if (Goodsid.Length > 2)
        //    {
        //        string  strSqlnumber = "";
        //        System.Data.IDataReader ddr;
        //        Goodsid = Goodsid.Remove(Goodsid.Length - 1);
        //        strSqlnumber = "select Goodsid from SupplyPlan_Goods where waitState=1  and Goodsid in(" + Goodsid + ") and  Projectid='" + projectid + "'";
        //        ddr = db.DataReader(strSqlnumber);
        //        while (ddr.Read())
        //        {
        //            nNumber++;
        //            //Goodsid += SysFun.ToTrim(dr["Goodsid"]) + ",";
        //        }
        //        ddr.Close();
        //        ddr.Dispose();
        //    }
          
           
        //    if (Goodsid.Length > 2)
        //    {
               
        //        str = "update SupplyPlan_Goods set historyPrice =convert(varchar(1000),historyprice) +'|'+ convert(varchar(1000),BidPrice), historytime =convert(varchar(1000),historytime) +'|'+ convert(varchar(1000),getdate()) where Goodsid in (" + Goodsid + ") and  Projectid='" + projectid + "'";
        //        str += " update SupplyPlan_Goods set BidPrice=convert(varchar(100),LatestPrice) where Goodsid in (" + Goodsid + ") and  Projectid='" + projectid + "'";
        //        str += " update SupplyPlan_Goods set LatestPrice='',waitState=0 where Goodsid in (" + Goodsid + ") and  Projectid='" + projectid + "'";
        //        str += " update tra_HosptaiPurchase_SupplyPlan_Goods set PurchasePrice=BidPrice where  PurchasePrice>BidPrice and Goodsid in (" + Goodsid + ")";
        //        db.Command(str);


        //        Response.Write("<script>alert('中标价格修改成功！！！影响" + nNumber.ToString() + "条记录');</script>");
        //        Button2.Enabled = false;
        //    }
        //    else
        //    {
        //        Response.Write("<script>alert('中标价格已修改！');</script>");
        //    }

        //    db.Close();
        //}
        if (Convert.ToInt32(Request.QueryString["ProjectId"]) ==10) 
        {
            string str = "", strSql, SupplyGoodsId = "";
            int nNumber = 0;
            Db db = new Db();
            System.Data.IDataReader dr;
            strSql = "select SupplyGoodsId from SupplyPlan_Goods where waitState=1 and  Projectid='" + projectid + "'";
            dr = db.DataReader(strSql);
            while (dr.Read())
            {
                nNumber++;
                SupplyGoodsId += SysFun.ToTrim(dr["SupplyGoodsId"]) + ",";
            }
            dr.Close();
            dr.Dispose();
            if (SupplyGoodsId.Length > 2)
            {
                SupplyGoodsId = SupplyGoodsId.Remove(SupplyGoodsId.Length - 1);
                str = "update SupplyPlan_Goods set historyPrice =convert(varchar(1000),historyprice) +'|'+ convert(varchar(1000),BidPrice), historytime =convert(varchar(1000),historytime) +'|'+ convert(varchar(1000),getdate()) where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update SupplyPlan_Goods set BidPrice=convert(numeric(20,4),LatestPrice) where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update SupplyPlan_Goods set LatestPrice='',waitState=0 where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update tra_HosptaiPurchase_SupplyPlan_Goods set PurchasePrice=BidPrice where  PurchasePrice>BidPrice and supplyGoodsId in (" + SupplyGoodsId + ")";
                db.Command(str);


                Response.Write("<script>alert('中标价格修改成功！！！影响" + nNumber.ToString() + "条记录');</script>");
                Button2.Enabled = false;
            }
            else
            {
                Response.Write("<script>alert('中标价格已修改！');</script>");
            }

            db.Close();
            SupplyGoodsId = "";
            int nNumber1 = 0;
            Db db1 = new Db();
            System.Data.IDataReader dr1;
            strSql = "select SupplyGoodsId from SupplyPlan_Goods where waitState1=1 and  Projectid='" + projectid + "'";
            dr1 = db1.DataReader(strSql);
            while (dr1.Read())
            {
                nNumber1++;
                SupplyGoodsId += SysFun.ToTrim(dr1["SupplyGoodsId"]) + ",";
            }
            dr1.Close();
            dr1.Dispose();
            if (SupplyGoodsId.Length > 2)
            {
                SupplyGoodsId = SupplyGoodsId.Remove(SupplyGoodsId.Length - 1);
                str = "update SupplyPlan_Goods set historyretailprice =convert(varchar(100),historyretailprice) +'|'+ convert(varchar(100),RetailPrice), histortyretailtime =convert(varchar(100),histortyretailtime) +'|'+ convert(varchar(100),getdate()) where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update SupplyPlan_Goods set RetailPrice=convert(varchar(100),MEMO4) where supplyGoodsId in (" + SupplyGoodsId + ")";
                str += " update SupplyPlan_Goods set MEMO4='',waitState1=0 where supplyGoodsId in (" + SupplyGoodsId + ")";
                //str += " update tra_HosptaiPurchase_SupplyPlan_Goods set PurchasePrice=BidPrice where  PurchasePrice>BidPrice and supplyGoodsId in (" + SupplyGoodsId + ")";
                db1.Command(str);


            Response.Write("<script>alert('最高零售价修改成功！！！影响" + nNumber1.ToString() + "条记录');</script>");
                Button2.Enabled = false;
            }
            else
            {
                Response.Write("<script>alert('最高零售价已修改！');</script>");
            }

            db1.Close();


        }
      


    }
    #region TraDes 成员

    public string Name
    {
        get { return "修改挂网目录列表"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "挂网目录管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            return _AccountType;
        }
    }

    #endregion

}
